﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FusionDotNet.Tests
{
    [TestClass]
    public class RowTests
    {
        private const int TEST_NAMES_TABLE = 377942;
        private static AuthenticationResult m_AuthResult;
        private const string USERNAME = "test@gmail.com";
        private const string PASSWORD = "***";

        private AuthenticationResult CreateAuthToken()
        {
            if (m_AuthResult == null)
            {
                AuthenticationCommand authCommand = new AuthenticationCommand(AccountTypeEnum.Google, USERNAME, PASSWORD, "fusiontables", "visionforms");
                m_AuthResult = authCommand.Execute();
            }
            return m_AuthResult;
        }

        [Timeout(60000), TestCategory("DELETE"), TestMethod]
        public void DeleteRowWithoutRowId()
        {

            AuthenticationResult authResult = CreateAuthToken();

            Assert.IsTrue(authResult.IsAuthenticated, "Authentication Failed");

            string commandText = "SELECT FirstName, LastName FROM " + TEST_NAMES_TABLE;
            SelectCommand command = new SelectCommand(commandText);
            command.AuthenticationToken = authResult.AuthenticationToken;
            SelectResult result = command.Execute();
            Assert.IsTrue(result.ResultTable.Rows.Count > 0);

            result.ResultTable.TableName = TEST_NAMES_TABLE.ToString();

            try
            {
                DeleteCommand deleteCommand = new DeleteCommand(result.ResultTable.Rows[0]);
                deleteCommand.AuthenticationToken = authResult.AuthenticationToken;
                CommandResult deleteResult = deleteCommand.Execute();
            }
            catch (InvalidOperationException)
            {
            }

        }
    }
}
